home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / basic / ubas830.zip / MALM.EXE / TSTCHN.UB < prev    next >
Text File  |  1990-08-22  |  2KB  |  48 lines

  1.    10   ' Test program for Chinese
  2.    15   dim A(20),B(20),M(20)
  3.    20   print "How many congruences: "
  4.    30   input N%
  5.    40   print "Enter the congruences A, B, M"
  6.    50   for I=1 to N%
  7.    60   input A(I),B(I),M(I)
  8.    70   next I
  9.    80   print "**********************"
  10.    90   print N%,"Congruences"
  11.   100   print:print "A","B","M"
  12.   110   for I=1 to N%
  13.   120   print A(I),B(I),M(I)
  14.   130   next I:print:print
  15.   140   gosub *Chinese(N%,&A(),&B(),&M(),&Soln,&Modulus)
  16.   150   print "modulus is: ",Modulus
  17.   160   if Modulus<>0 then print "Solution: ";Soln
  18.   170   end
  19.  1170   *Chinese(N%,&A(),&B(),&M(),&Soln,&Modulus)
  20.  1180   ' Chinese remaindering algorithm, modeled on the Pascal version.
  21.  1190   ' 21 August 1990
  22.  1200   local C,Ca,Te,Ta,I%,Aa,Bb
  23.  1210   if N%<=0 then Modulus=0:return endif
  24.  1220   gosub *Fcgcd(A(1),M(1),&C,&Ca)
  25.  1230   Te=B(1)\C:if res then Modulus=0:return endif
  26.  1240   Soln=Te*Ca:Modulus=M(1)\C
  27.  1250   for I%=2 to N%
  28.  1260   Aa=A(I%)*Modulus:Bb=B(I%)-A(I%)*Soln
  29.  1270   gosub *Fcgcd(Aa,M(I%),&C,&Ca)
  30.  1280   Te=Bb\C:if res then Modulus=0:cancel for:return endif
  31.  1290   Soln=Soln+Modulus*Te*Ca
  32.  1300   Modulus=Modulus*(M(I%)\C)
  33.  1310   Soln=Soln@Modulus
  34.  1320   next I%
  35.  1330   return ' End of subroutine Chinese
  36.  2330   *Fcgcd(A,B,&C,&Ca)
  37.  2340   ' Gcd with one coefficient.  c is the gcd of a and b.
  38.  2350   ' ca*a + cb*b = c for some cb    16 April 1990
  39.  2360   local R,Q,T,Af,Bf,B1
  40.  2370   Ca=1:B1=0:Af=A:Bf=B
  41.  2380   while B
  42.  2390   T=B:Q=A\B:B=res
  43.  2400   R=Ca-Q*B1:Ca=B1:B1=R:A=T
  44.  2410   wend
  45.  2420   C=A
  46.  2430   if C<0 then neg C:neg Ca endif
  47.  2440   return:' End of procedure Fcgcd.
  48.